package com.huicoo.forestmanager.ui.patrol.forestCampTask;

import android.util.Log;
import android.widget.ImageView;
import android.widget.TextView;

import com.huicoo.forestmanager.R;
import com.huicoo.forestmanager.base.BaseActivity;
import com.huicoo.forestmanager.util.LogUtils;
import com.tianditu.android.maps.GeoPoint;
import com.tianditu.android.maps.MapController;
import com.tianditu.android.maps.MapView;
import com.tianditu.android.maps.MyLocationOverlay;

import butterknife.BindView;
import butterknife.OnClick;

import static com.tianditu.android.maps.MapView.LOGO_RIGHT_BOTTOM;
import static com.tianditu.android.maps.MapView.TMapType.MAP_TYPE_IMG;
import static com.tianditu.android.maps.MapView.TMapType.MAP_TYPE_VEC;

public abstract class BaseForestCampMapActivity extends BaseActivity {
    @BindView(R.id.map_view)
    MapView map_view;

    @BindView(R.id.positioning)
    ImageView positioning;

    @BindView(R.id.subtract)
    ImageView subtract;

    @BindView(R.id.add)
    ImageView add;

    @BindView(R.id.map_type_iv)
    ImageView map_type_iv;

    @BindView(R.id.map_type_tv)
    TextView map_type_tv;

    private MapController mMapController;
    private MyLocationOverlay myLocationOverlay;

    @Override
    protected void initView() {
        //初始化地图设置
        initMapView();
    }

    //初始化地图设置
    private void initMapView() {
        //设置启用内置的缩放控件
        map_view.setBuiltInZoomControls(false);

        //得到mMapView的控制权,可以用它控制和驱动平移和缩放
        mMapController = map_view.getController();

        //创建MyLocationOverlay
        myLocationOverlay = new MyLocationOverlay(getApplicationContext(), map_view);

        //启用指南针位置更新
        myLocationOverlay.enableCompass();

        //启用我的位置
        myLocationOverlay.enableMyLocation();

        map_view.addOverlay(myLocationOverlay);

        //设置地图zoom级别
        mMapController.setZoom(15);

        //设置Logo显示位置.
        map_view.setLogoPos(LOGO_RIGHT_BOTTOM);

        //设置比例尺变化的监听
        map_view.setScaleChangeListener(new MapView.OnScaleChangeListener() {
            @Override
            public void onScaleChange() {
                Log.e("地图", "比例尺变化--" + map_view.getZoomLevel());
            }
        });

        if (map_view.getMapType() == MAP_TYPE_IMG) {
            map_view.setMapType(MAP_TYPE_VEC);
        }

        //初始化先定位一下
        GeoPoint point = myLocationOverlay.getMyLocation();
        LogUtils.v("gogogo init point = " + (point == null));
    }


    //定位到当前位置
    protected GeoPoint locateCurrentLocation(GeoPoint location) {
        GeoPoint myLocation;
        if (location == null) {
            //获得当前位置
            myLocation = myLocationOverlay.getMyLocation();
        } else {
            myLocation = location;
        }

        //设置地图中心点
        mMapController.setCenter(myLocation);

        //动画移动到当前位置
        mMapController.animateTo(myLocation);

        return myLocation;
    }

    //经纬度转后台用格式，除1E6 如24519952/1E6
    protected String formatPoint(int value) {
        return String.valueOf((double) value / 1E6);
    }

    //获得当前定位精确度
    protected String getCurrentAccuracy() {
        return String.valueOf((int)myLocationOverlay.getAccuracy());
    }

    //定位
    @OnClick(R.id.positioning)
    public void positioning() {
        //定位到当前位置
        locateCurrentLocation(null);
    }


    //减
    @OnClick(R.id.subtract)
    public void subtract() {
        // 缩小一级
        mMapController.zoomOut();

    }

    //加
    @OnClick(R.id.add)
    public void add() {
        // 放大一级
        mMapController.zoomIn();
    }

    @OnClick(R.id.map_type_ll)
    public void mapTypeLl() {
        //当前是影像
        if (map_view.getMapType() == MAP_TYPE_IMG) {
            map_type_iv.setImageResource(R.mipmap.icon_satellite);
            map_view.setMapType(MAP_TYPE_VEC);
            map_type_tv.setText("影像");
        } else if (map_view.getMapType() == MAP_TYPE_VEC) {
            map_type_iv.setImageResource(R.mipmap.icon_plane);
            //矢量
            map_view.setMapType(MAP_TYPE_IMG);
            map_type_tv.setText("平面");
        }
    }

    protected void setZoom(int level) {
        mMapController.setZoom(level);
    }
}
